package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Transactional
    @Override
    public Page<Setmeal> findByPage(String name, Integer pageNum, Integer pageSize) {
        //模糊查询
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //设置分页条件
        Page<Setmeal> page = new Page<>(pageNum, pageSize);

        //执行条件的分页查询
        page = setmealMapper.selectPage(page, setmealLambdaQueryWrapper);

        //遍历
        List<Setmeal> records = page.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            for (Setmeal record : records) {
                //查询
                Category category = categoryMapper.selectById(record.getCategoryId());
                record.setCategoryName(category.getName());

                //根据id查询
                LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
                setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, record.getId());
                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(setmealDishLambdaQueryWrapper);
                record.setSetmealDishes(setmealDishes);
            }
        }
        return page;
    }

    @Transactional
    @Override
    public void save(Setmeal setmeal) {
        //保存基本信息
        setmealMapper.insert(setmeal);

        //获取setmeal中的setmealDishs，遍历  保存到中间表
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {

                setmealDish.setSetmealId(setmeal.getId());
                //保存
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Transactional
    @Override
    public void deleteByIds(Long[] ids) {
        //判断删除的套餐是否有还在销售，如果有直接报错
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId, ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        Integer count = setmealMapper.selectCount(setmealLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("你要删除的套餐中还有在销售中，无法删除");
        }

        //先删除套餐中所有符合条件的数据
        setmealMapper.deleteBatchIds(Arrays.asList(ids));

        //删除套餐中间表所有符合条件的数据
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(setmealDishLambdaQueryWrapper);
    }

    @Transactional
    @Override
    public Setmeal findById(Long id) {

        //执行查询
        Setmeal setmeal = setmealMapper.selectById(id);

        //查询setmealDishes
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(setmealDishLambdaQueryWrapper);
        setmeal.setSetmealDishes(setmealDishes);

        //查询categoryName
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(Category::getId, setmeal.getCategoryId());
        Category category = categoryMapper.selectOne(categoryLambdaQueryWrapper);
        if (category != null) {
            setmeal.setCategoryName(category.getName());
        }
        return setmeal;
    }

    @Transactional
    @Override
    public void update(Setmeal setmeal) {
        //更新数据
        setmealMapper.updateById(setmeal);

        //删除setmealDishes   categoryName 表中数据
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(setmealDishLambdaQueryWrapper);

        //更新setmealDishes   categoryName 表中数据
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public void updateByStatus(Integer sales, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(sales);
            setmealMapper.updateById(setmeal);
        }
    }

    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        //创建条件
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, categoryId);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, status);
        //执行查询
        return setmealMapper.selectList(setmealLambdaQueryWrapper);
    }

    @Override
    public List<SetmealDish> findImg(Long id) {
        //创建条件
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(setmealDishLambdaQueryWrapper);

        for (SetmealDish setmealDish : setmealDishes) {
            LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishLambdaQueryWrapper.eq(Dish::getId,setmealDish.getDishId());
            List<Dish> dishList = dishMapper.selectList(dishLambdaQueryWrapper);
            for (Dish dish : dishList) {
                setmealDish.setImage(dish.getImage());
                setmealDish.setDescription(dish.getDescription());
            }
        }

        return setmealDishes;
    }
}
